/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.controller;

import com.cxhd.dc.DataConfig;
import com.cxhd.dc.websocket.ConnectServer;
import com.cxhd.dc.websocket.Message;
import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class IndexController extends Controller {

    public void index() {
        render("/index.jsp");
    }

    public void saveConfig() {
        Map<String, String[]> map = getParaMap();
        try {
            for (String key : map.keySet()) {

                Db.use("dcmris_en").update("replace into dataconfig set name='" + key + "',value='" + getPara(key) + "'");
            }
            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
    }

    public void getConfig() {
        List<Record> list = Db.use("dcmris_en").find("select * from dataconfig");
        Map<String, String> map = new HashMap<String, String>();
        if (!list.isEmpty()) {
            for (Record re : list) {
                map.put(re.getStr("name"), re.getStr("value"));
            }
        }
        renderJson(map);
    }
    
    public void sendGetRpt(){
        String study_iuid= getPara("study_iuid");
        if(study_iuid!=null){
            Message msg = new Message();
            msg.setTo(DataConfig.getInstance().getSN());
            msg.setBody(study_iuid);
            msg.setFrom(DataConfig.getInstance().getLocalhostSN());
            ConnectServer.getIntance().sendMessage(new Gson().toJson(msg));
            renderText("true");
        }
        else{
            renderText("false");
        }
    }

}
